/*
 * EditSettings.java
 *
 * Created on September 20, 2007, 7:17 PM
 */

/**
 *
 * Dialog to edit settings.
 *
 * @author  pquiring
 */

public class EditSettings extends javax.swing.JDialog {

  /** Creates new form EditSettings */
  private EditSettings(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    setTitle("Edit Settings");
    setComponentOrientation(((parent == null) ? javax.swing.JOptionPane.getRootFrame() : parent).getComponentOrientation());
    if (parent != null) setLocationRelativeTo(parent);
    load();
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    bOk = new javax.swing.JButton();
    bCancel = new javax.swing.JButton();
    sFontSize = new javax.swing.JSpinner();
    jLabel1 = new javax.swing.JLabel();

    FormListener formListener = new FormListener();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setResizable(false);
    addKeyListener(formListener);

    bOk.setMnemonic('o');
    bOk.setText("OK");
    bOk.addActionListener(formListener);
    bOk.addKeyListener(formListener);

    bCancel.setMnemonic('c');
    bCancel.setText("Cancel");
    bCancel.addActionListener(formListener);
    bCancel.addKeyListener(formListener);

    sFontSize.addChangeListener(formListener);
    sFontSize.addKeyListener(formListener);

    jLabel1.setText("Font Size");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(jLabel1)
          .addComponent(bOk))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(bCancel)
          .addComponent(sFontSize, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(sFontSize, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(bOk)
          .addComponent(bCancel))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
  }

  // Code for dispatching events from components to event handlers.

  private class FormListener implements java.awt.event.ActionListener, java.awt.event.KeyListener, javax.swing.event.ChangeListener {
    FormListener() {}
    public void actionPerformed(java.awt.event.ActionEvent evt) {
      if (evt.getSource() == bOk) {
        EditSettings.this.bOkActionPerformed(evt);
      }
      else if (evt.getSource() == bCancel) {
        EditSettings.this.bCancelActionPerformed(evt);
      }
    }

    public void keyPressed(java.awt.event.KeyEvent evt) {
      if (evt.getSource() == EditSettings.this) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == bOk) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == bCancel) {
        EditSettings.this.anykey(evt);
      }
      else if (evt.getSource() == sFontSize) {
        EditSettings.this.anykey(evt);
      }
    }

    public void keyReleased(java.awt.event.KeyEvent evt) {
    }

    public void keyTyped(java.awt.event.KeyEvent evt) {
    }

    public void stateChanged(javax.swing.event.ChangeEvent evt) {
      if (evt.getSource() == sFontSize) {
        EditSettings.this.sFontSizeStateChanged(evt);
      }
    }
  }// </editor-fold>//GEN-END:initComponents

  private void anykey(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_anykey
    int keyCode = evt.getKeyCode();
    int mods = evt.getModifiers();
    if (keyCode == java.awt.event.KeyEvent.VK_ESCAPE && mods == 0) {
      bCancelActionPerformed(null);
    }
    if (keyCode == java.awt.event.KeyEvent.VK_ENTER && mods == 0) {
      bOkActionPerformed(null);
    }
  }//GEN-LAST:event_anykey

  private void sFontSizeStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_sFontSizeStateChanged
    int tmp = ((Integer)sFontSize.getValue()).intValue();
    if (tmp < MIN_FONTSIZE) sFontSize.setValue(MIN_FONTSIZE);
    if (tmp > MAX_FONTSIZE) sFontSize.setValue(MAX_FONTSIZE);
  }//GEN-LAST:event_sFontSizeStateChanged

  private void bCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCancelActionPerformed
    dispose();
  }//GEN-LAST:event_bCancelActionPerformed

  private void bOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bOkActionPerformed
    save();
    dispose();
  }//GEN-LAST:event_bOkActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton bCancel;
  private javax.swing.JButton bOk;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JSpinner sFontSize;
  // End of variables declaration//GEN-END:variables

  private static final int MIN_FONTSIZE = 8;
  private static final int MAX_FONTSIZE = 24;
  private static final int MIN_TABSIZE = 2;
  private static final int MAX_TABSIZE = 8;

  public static void editSettings(java.awt.Frame parent) {
    EditSettings dialog = new EditSettings(parent, true);
    dialog.setVisible(true);
  }

  private void load() {
    sFontSize.setValue(Settings.fontSize);
  }

  private void save() {
    Settings.fontSize = ((Integer)sFontSize.getValue()).intValue();
  }

}
